//
//  STDSMosaicEffect.hpp
//  STDRenderSDK
//
//  Created by 陈学明 on 2023/3/20.
//

#ifndef STDSMosaicEffect_hpp
#define STDSMosaicEffect_hpp

#include <stdio.h>
#include "STDSBaseEffect.hpp"

/// 马赛克效果
class STDSMosaicEffect: public STDSBaseEffect {
public:
    STDSMosaicEffect() {
        mEffectType = STDSEffectTypeMosaic;
    }
    ~STDSMosaicEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
        STDSBaseEffect::initGL();
        mMosaicProgram = mContext->getProgram(kShaderSRCMosaic);
    }
    
    void setIntArtuments(const char *name, int *intValue, int n) override {
        mCount = *intValue;
    }
    
    void setFloatArguments(const char *name, float *floatValue, int n) override {
        mCount = floor(*floatValue);
    }
    
public:
    void draw() override {
        mMosaicProgram->use();
        if (mTexture != nullptr) {
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, mTexture->getId());
            mMosaicProgram->setUniform(mMosaicProgram->getUniform("uTexture"), 0);
            mMosaicProgram->setUniform2f(mMosaicProgram->getUniform("uParams"), mCount, mCount);
        }
        mAtbRect.setPosVex(mMosaicProgram->getAttrib("aPos"));
        mAtbRect.setPosTex(mMosaicProgram->getAttrib("aTexCoord"));
        mAtbRect.draw();
        mMosaicProgram->unuse();
    }

private:
    float mCount{10};
    shared_ptr<STDSShaderProgram> mMosaicProgram{nullptr};
};
#endif /* STDSMosaicEffect_hpp */
